package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 合同Service业务层处理
 * 
 * @author alexis
 * @date 2025-02-24
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同
     * 
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return contractMapper.insert(contract);
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除合同
     * 
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateStatus() {
        // 未生效0-->已生效1：开始时间 <= 当前时间 <= 结束时间
        LocalDateTime now = LocalDateTime.now();
        // 查询已生效，但状态为0的合同列表
        List<Contract> startContracts = this.lambdaQuery()
                .eq(Contract::getStatus, 0)
                .le(Contract::getStartDate, now)
                .ge(Contract::getEndDate, now)
                .list();
        // 修改状态
        if(CollUtil.isNotEmpty(startContracts)){
            startContracts.forEach(o -> o.setStatus(1));
            boolean flag = this.updateBatchById(startContracts);
            if(!flag){
                throw new BaseException("批量更改生效合同状态失败");
            }
        }
        // 查询已失效，但状态为1的合同列表
        List<Contract> endContracts = this.lambdaQuery()
                .eq(Contract::getStatus, 1)
                .lt(Contract::getEndDate, now)
                .list();
        // 修改状态
        if(CollUtil.isNotEmpty(endContracts)){
            endContracts.forEach(o -> o.setStatus(3));
            boolean flag = this.updateBatchById(endContracts);
            if(!flag){
                throw new BaseException("批量更改失效合同状态失败");
            }
        }
    }
}
